package com.java46.front.dessertType.dao;

import com.java46.front.base.util.HibernateUtils;
import com.java46.front.dessert.entity.DessertEntity;
import com.java46.front.dessertType.entity.DesserttypeEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

public class DessertTypeDao {

        //查询甜品类型
        public List<DesserttypeEntity> getAllDessertType(){
                List<DesserttypeEntity> list = new ArrayList<DesserttypeEntity>();
                Configuration configure = new Configuration().configure();
                SessionFactory sessionFactory = configure.buildSessionFactory();
                Session session = sessionFactory.openSession();
                Query query = session.createQuery("from com.java46.front.dessertType.entity.DesserttypeEntity");
                list = query.list();
                session.close();
                sessionFactory.close();
                return list;
        }

        //添加甜品类型
        public void addDessertType(DesserttypeEntity dessertType){
                Session session = HibernateUtils.getSession(); //拿到session
                session.save(dessertType);//添加
                session.beginTransaction().commit();//提交
                session.close();//关闭
        }

        //删除甜品类型
        public  void deleteDessertType(int dessertTypeId){
                Session session = HibernateUtils.getSession(); //拿到session
                Query query = HibernateUtils.getQuery("DesserttypeEntity from dessertType where typeId=?1");
                query.setParameter(1,dessertTypeId);
                query.executeUpdate();
                session.beginTransaction().commit();
                session.close();
        }
        //通过ID查询甜品类型
        public  List<DessertEntity> queryDessertTypeId(int dessertTypeId){
            List<DessertEntity> list=new ArrayList<>();
            Session session = HibernateUtils.getSession();
            NativeQuery nativeQuery = HibernateUtils.getNativeQuery("SELECT * FROM dessert WHERE dessertType=?1;");
            nativeQuery.setParameter(1, dessertTypeId);
            list = nativeQuery.list();
            session.close();
            return list;
        }
}
